<?php


namespace App\Models\Admin;


use App\Log\AdminShopOrderLog;

class AdminShopOrder extends Base
{
    const OPERATE_PID = 6;
    const MODEL = 'admin_shop_order';

    protected $table = 'admin_shop_order';
    protected $guarded = [];
    protected $dateFormat = 'U';

    public function company()
    {
        return $this->belongsTo(Company::class,'company_id','id');
    }

    public static function getList($data=[],$skip=0,$size=10)
    {
        $select=self::getWhere($data);
        $list=$select->with([
            'company:id,company_name',
        ])
            ->select('id','company_id','order_sn','order_status','order_money','receiving_name','receiving_mobile','receiving_address','created_at')
            ->skip($skip)->take($size)
            ->orderBy('id','desc')
            ->get()->transform(function ($i)
            {
                $i->company_name=is_null($i->company)?'':$i->company->company_name;
                unset($i->company);
                return $i;
            });
        return $list;
    }

    public static function getCount($data=[])
    {
        $select=self::getWhere($data);
        $count=$select->count();
        return $count;
    }

    public static function getWhere($data=[])
    {
        $select=self::where([]);
        if(!empty($data['create_start_time']) && !empty($data['create_end_time']))
        {
            $select = $select->whereBetween('created_at',date_assemble($data['create_start_time'],$data['create_end_time']));
        }
        if(!empty($data['key']))
        {
            $key='%'.$data['key'].'%';
            $company_ids=Company::where('company_name','like',$key)->pluck('id')->toArray();
            $select=$select->where(function($query) use ($key,$company_ids)
            {
                $query->whereIn('company_id',$company_ids)
                    ->orWhere('order_sn','like',$key)
                    ->orWhere('receiving_name','like',$key)
                    ->orWhere('receiving_mobile','like',$key);
            });
        }
        if(isset($data['order_status']))
        {
            $select=$select->where('order_status',$data['order_status']);
        }
        return $select;
    }

    //根据id获取订单详情
    public static function getDetailById($id)
    {
        $info=self::with([
            'company:id,company_name',
        ])
            ->where('id',$id)
            ->first();
        if($info)
        {
            $info->company_name=is_null($info->company)?'':$info->company->company_name;
            unset($info->company);
        }
        return $info;
    }

    public static function addLog($pre,$next,$type,$method='')
    {
        $info = AdminOperateModule::where('pid',self::OPERATE_PID)->where('type',$type)->where('model',self::MODEL)->when($method,function ($query)use ($method){
            $query->where('method',$method);
        })->first();
        if (!empty($info)){
            $adminLog = new AdminShopOrderLog($pre,$next,$info);
            $adminLog->addLog();
        }
    }

}

